package com.dc.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.project.entity.bo.TemplateBo;
import com.dc.project.entity.map.TemplateMapVo;
import com.dc.project.entity.po.Template;
import com.dc.project.entity.query.TemplateQuery;
import com.dc.project.entity.vo.TemplateVo;
import com.dc.project.mapper.TemplateMapper;
import com.dc.project.service.TemplateService;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * TemplateServiceImpl
 *
 * @author chenxueli
 * @date 2025-03-19 10:40:23
 */
@Slf4j
@Service
public class TemplateServiceImpl extends AbstractBaseService<
        Template,
        TemplateBo,
        TemplateVo,
        TemplateMapVo,
        TemplateQuery,
        TemplateMapper
        > implements TemplateService {

    @Resource
    private TemplateMapper templateMapper;

    /**
     * 获取查询条件
     *
     * @param query 查询条件
     * @return 查询条件对象
     */
    @Override
    public LambdaQueryWrapper<Template> lambdaQueryWrapper(TemplateQuery query) {
        return super.lambdaQueryWrapper(query)
                .like(StringUtils.isNotBlank(query.getTitle()), Template::getTitle, query.getTitle())
                .eq(StringUtils.isNotBlank(query.getKey()), Template::getKey, query.getKey())
                .orderByDesc(Template::getGmtCreate)
                .orderByDesc(Template::getId);
    }

    /**
     * 根据模板key获取模板
     *
     * @param key 模板key
     * @return 模板
     */
    @Override
    public Template getPoByTemplateKey(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        var wrapper = lambdaQueryWrapper()
                .eq(Template::getKey, key)
                .orderByDesc(Template::getGmtCreate)
                .orderByDesc(Template::getId);
        return templateMapper.selectOne(wrapper);
    }

}
